import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class htmlReader {
    private static Pattern patternImg = Pattern.compile("<img((.+?)src\\s*=\\s*(((\\\"|\\')\\s*([^\\\\3]*?)\\s*(\\\\3))|([^\\s|\\\"|\\'|>]*))[^>]*)>", Pattern.MULTILINE);
    private String outputFile = "output.txt";
    private static  String htmlParentDir;

    public static String getImg(String page) {
        if (page == null)
            return null;
        Matcher matcherA = patternImg.matcher(page);
        String it = null;
        int index = 0;
        StringBuffer sb = new StringBuffer();
        while (true) {
            if (matcherA.find()) {
                it = matcherA.group();
                index = it.indexOf("src=");
                it = it.substring(index + 5);
                index = it.indexOf("\"");
                sb.append(it.substring(0, index)).append('\n');
//                  sb.append(it).append('\n');
            } else {
                return sb.toString();
            }
        }
    }

    public void read(String filename) {

        File output = new File(outputFile);
        try {
            output.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        File input = new File(filename);
        htmlParentDir = input.getParent();
        Scanner scan = null;
        String res = null;
        try {
            scan = new Scanner(input);
        } catch (
                FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        PrintWriter pwriter = null;
        try {
            pwriter = new PrintWriter(output);
            StringBuffer sb = new StringBuffer();

            while (scan.hasNextLine()) {
                sb.append(scan.nextLine());

            }
            res = getImg(sb.toString());
            if (res != null) {
                pwriter.print(res + '\n');
            }
        } catch (
                IOException e) {
            e.printStackTrace();  
        }
        finally {
            pwriter.close();
        }

    }

    public void copyImages() {
        File file = new File("tmpImages");
        file.mkdir();
        File input = new File(outputFile);
        Scanner scan = null;
        Integer i=1;
        try {
            scan = new Scanner(input);
            String nextName;
            while (scan.hasNextLine()) {
                nextName = scan.nextLine();
                if(nextName.startsWith("http")){
                    try {
                        URL imageUrl = new URL(nextName);
                        BufferedInputStream in = new BufferedInputStream(imageUrl.openStream());
                        nextName = i.toString() + ".jpg";
                        FileOutputStream fos = new FileOutputStream("tmpImages/"+nextName);
                        ++i;

                        BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                        byte data[] = new byte[1024];
                        int len = 0;
                        while((len = in.read(data, 0, 1024))>=0)
                        {
                            bout.write(data, 0, len);
                        }
                        bout.close();
                        in.close();

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    continue;
                }
                File nextFile = new File(htmlParentDir+'/'+nextName);
                nextFile.renameTo(new File("tmpImages/" + nextName));
            }
        } catch (
                FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        finally {
            scan.close();
        }

    }

    public String getOutputFileName() {
        return outputFile;
    }

    public String getOutputFile() {
        return outputFile;
    }
}
